package com.free.bsf.metric.controller;

import com.free.bsf.core.base.Ref;
import com.free.bsf.core.db.DbHelper;
import com.free.bsf.core.util.StringUtils;
import com.free.bsf.metric.base.Config;
import com.free.bsf.metric.base.Pager1;
import com.free.bsf.metric.base.SpringMvcController;
import com.free.bsf.metric.base.User;
import com.free.bsf.metric.dao.model.auto.tb_front_metric_model;
import com.free.bsf.metric.dao.tb_front_metric_dal;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Slf4j
@Controller
@RequestMapping("/frontmetric")
public class FrontMetricController extends SpringMvcController {
    @RequestMapping("/index")
    public ModelAndView index(Integer pageindex, Integer pagesize) {
        val pageIndex2 = (pageindex == null ? 1 : pageindex);
        val pageSize2 = (pagesize == null ? 10 : pagesize);
        return pageVisit((m) -> {
            //val create_user2 = create_user==null? User.getCurrent().getUsername():create_user;
            html//.s2("title", StringUtils.nullToEmpty(title)).s2("create_user", create_user2)
                    .s2("pageindex", pageindex).s2("pagesize", pagesize);
                    Ref<Integer> totalSize = new Ref<>(0);
                    val list = DbHelper.get(Config.mysqlDataSource(), c -> {
                        return new tb_front_metric_dal().searchPage(c, pageIndex2, pageSize2, totalSize);
                    });
                    new Pager1(pageIndex2, totalSize.getData()).setPageSize(pageSize2).out();
                    request.setAttribute("model", list);
                }
        );
    }

    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
        html.s2("id", id);
        return pageVisit((m) -> {
            val model = DbHelper.get(Config.mysqlDataSource(), c -> {
                return new tb_front_metric_dal().get(c, id);
            });
            val temp = new tb_front_metric_model();
            request.setAttribute("model", model==null?temp:model);
        });
    }

    @RequestMapping("/save")
    public ModelAndView save(Integer id, String remark,
                             String version_name,String main_script,String tool_script
    ) {
        return jsonVisit((m) -> {
            DbHelper.call(Config.mysqlDataSource(), c -> {
                tb_front_metric_model model = new tb_front_metric_dal().get(c, id);
                if (model == null) {
                    model = new tb_front_metric_model();
                    model.create_time = new Date();
                    model.update_time = new Date();
                    model.isUse=0;
                }else {
                    model.update_time = new Date();
                }
                model.main_script=main_script;
                model.tool_script=tool_script;
                model.remark = remark;
                model.version_name = version_name;
                if(model.id==null||model.id==0){
                    new tb_front_metric_dal().add(c,model);
                }else {
                    new tb_front_metric_dal().edit(c,model);
                }
            });
            return true;
        });
    }

    @RequestMapping("/del/")
    public ModelAndView del(Integer id) {
        return jsonVisit((m) -> {
            DbHelper.call(Config.mysqlDataSource(), c -> {
                tb_front_metric_model model = new tb_front_metric_dal().get(c, id);
                if(model!=null) {
                    new tb_front_metric_dal().delete(c, id);
                }
            });
            return true;
        });
    }

    @RequestMapping("/setUse/")
    public ModelAndView setUse(Integer id) {
        return jsonVisit((m) -> {
            DbHelper.call(Config.mysqlDataSource(), c -> {
                tb_front_metric_model use = new tb_front_metric_dal().getUse(c);
                if(use!=null) {
                    new tb_front_metric_dal().setUse(c, use.id,false);
                }

                tb_front_metric_model model = new tb_front_metric_dal().get(c, id);
                if(model!=null) {
                    new tb_front_metric_dal().setUse(c, id,true);
                }
            });
            return true;
        });
    }
}
